#!/bin/bash
#Desc:install mysql 5.7.31 for glibc version
#author:zhang huan huan
#update:2020 08-31 09-03
#usage:$1 glic.tar.gz path $2 basedir  $3slave or master $4 set server-id
#checknetwork
checknetwork(){
echo "正在检查网络。。。"
ping -c 3 114.114.114.114 &>/dev/null
if [ $? -eq 0 ]
then
       	echo "网络已经准备好！！！"
else
       	echo "请先检查网络"
       	exit
fi
}
#install rely software
install_rely(){ 
	yum -y install  libaio
}

#unpack glibc tar.gz file
#$1,gz file path $2,basedir
unpack(){
	if [  ! -d $2  ]; then
		mkdir $2	
	fi
	tar -zxf $1 -C $2
	temppath=$2'/*/*'
	mv  $temppath $2
}

add_group_user(){
	cat /etc/group |grep ^mysql &>/dev/null
	if [ $? -gt 0 ]; then
		groupadd mysql 
	fi 
	cat /etc/passwd |grep ^mysql &>/dev/null
	if [ $? -gt 0 ]; then 
		useradd -r -g mysql -s /usr/bin/nologin mysql 	
	fi 
}

#$1 basedir
mysql_files_own(){
	cd $1
	mkdir mysql-files
	chown -R mysql:mysql mysql-files
	chmod 750 mysql-files
}

#init and configure for mysql
init_master_configure(){
	cd $1
	cp support-files/mysql.server /etc/init.d/mysqld
	sed -i "/^basedir=/s#basedir=#basedir=$1#" /etc/init.d/mysqld
	sed -i "/^datadir=/s#datadir=#datadir=$1/data#" /etc/init.d/mysqld
	rm -f /etc/my.cnf
	touch my.cnf
	cat >> my.cnf << EOF
	[mysqld]
	basedir=$1
	datadir=$1/data
	socket=/tmp/mysql.sock
	port=3306
	log_error=$1/data/mysql.err
	server-id=$2
	log-bin=$1/data/binlog
	character_set_server=utf8mb4
	gtid-mode=on
	log-slave-updates=1
	enforce-gtid-consistency
EOF
	echo "export PATH=$PATH:$1/bin" >> /etc/profile
	source /etc/profile
	
}

#init and configure for mysql
init_slave_configure(){
	cd $1
	cp support-files/mysql.server /etc/init.d/mysqld
	sed -i "/^basedir=/s#basedir=#basedir=$1#" /etc/init.d/mysqld
	sed -i "/^datadir=/s#datadir=#datadir=$1/data#" /etc/init.d/mysqld
	rm -f /etc/my.cnf
	touch my.cnf
	cat >> my.cnf << EOF
	[mysqld]
	basedir=$1
	datadir=$1/data
	socket=/tmp/mysql.sock
	port=3306
	log_error=$1/data/mysql.err
	server-id=$2 
	log-bin=$1/data/binlog
	relay-log=$1/data/relaylog
	character_set_server=utf8mb4
	gtid-mode=on
	log-slave-updates=1
	enforce-gtid-consistency
	skip-slave-start
EOF
	echo "export PATH=$PATH:$1/bin" >> /etc/profile
	source /etc/profile
	
}

#$1 basedir
start(){
	mysqld --initialize --user=mysql --basedir=$1 --datadir=$1/data  &> result.txt
	pkill mysqld
	service mysqld start
	if [ $? -eq 0 ]; then
		mysql_ssl_rsa_setup  --datadir=$1/data 
		chkconfig --add mysqld
		
	fi
	
}

#init mysql and set root passwd ( $2)
#$1 basedir
mysql_secure_init(){
	cd $1
	firspwd=`cat result.txt | grep "localhost:"| awk -F "localhost:" '{print $2}'|tr -d " "`
echo
mysql_secure_installation <<EOF
$firspwd
$2
$2


y
y
y
y
EOF
}

main(){
	case "$3" in
		"slave")
		checknetwork
		install_rely
		unpack $1 $2
		add_group_user
		mysql_files_own $2
		init_slave_configure $2 $4
		 ;;
		 "master")
		checknetwork
		install_rely
		unpack $1 $2
		add_group_user
		mysql_files_own $2
		init_master_configure $2 $4
		start $2
		 ;;
		*)
		 ;;
	esac
}
 
main $1 $2 $3 $4
